float4x4 WorldViewProj : WorldViewProjection;
float4x4 World: World;
float4x4 WorldInverseTranspose : WorldInverseTranspose;

float4 LightPosition0 : LightPosition0;
float4 LightDiffuse0 : LightDiffuse0;
float4 LightSpecular0 : LightSpecular0;
float3 LightAttenuation0: LightAttenuation0;

float4x4 shadowTransform : MatrixParameter0;
float4x4 shadowTransform2 : MatrixParameter1;
float4x4 shadowTransform3 : MatrixParameter2;
float4x4 shadowTransform4 : MatrixParameter3;

float Time: Time;

struct VS_INPUT{
	float3 Position: POSITION;
	float2 UV: TEXCOORD0;
	float3 Normal: NORMAL;
	float4 Color: COLOR0;
};
struct VS_OUTPUT{
    float4 pos:POSITION;
    float2 UV: TEXCOORD0;
    float2 CellUV: TEXCOORD1;
    float4 Color : TEXCOORD2;
	float4 ShadowCoord: TEXCOORD3;
    float4 ShadowCoord2: TEXCOORD4;
    float4 ShadowCoord3: TEXCOORD5;
    float4 ShadowCoord4: TEXCOORD6;
    
};

texture CellTexture: TEXTURE0;
texture MarkTexture: TEXTURE1;
texture ShadowTexture: TEXTURE2;
texture ShadowTexture2: TEXTURE3;
texture ShadowTexture3: TEXTURE4;
texture ShadowTexture4: TEXTURE5;

sampler2D CellSampler = sampler_state
{
    Texture = (CellTexture);
    MIPFILTER = LINEAR;
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
	ADDRESSU = CLAMP;
	ADDRESSV= CLAMP;
};

sampler2D MarkSampler = sampler_state
{
    Texture = (MarkTexture);
    MIPFILTER = LINEAR;
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
	ADDRESSU = WRAP;
	ADDRESSV=WRAP;
};

sampler2D ShadowSampler = sampler_state
{
    Texture = (ShadowTexture);
    MIPFILTER = NONE;
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
	ADDRESSU = CLAMP;
	ADDRESSV=CLAMP;
};

sampler2D ShadowSampler2 = sampler_state
{
    Texture = (ShadowTexture2);
    MIPFILTER = NONE;
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
	ADDRESSU = CLAMP;
	ADDRESSV=CLAMP;
};
sampler2D ShadowSampler3 = sampler_state
{
    Texture = (ShadowTexture3);
    MIPFILTER = NONE;
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
	ADDRESSU = CLAMP;
	ADDRESSV=CLAMP;
};
sampler2D ShadowSampler4 = sampler_state
{
    Texture = (ShadowTexture4);
    MIPFILTER = NONE;
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
	ADDRESSU = CLAMP;
	ADDRESSV=CLAMP;
};

VS_OUTPUT mainVS(VS_INPUT input){
    VS_OUTPUT output;
    output.pos=mul(float4(input.Position, 1.0), WorldViewProj);
    output.Color = input.Color;
	output.ShadowCoord = mul (float4(input.Position,1),shadowTransform);
    output.ShadowCoord2 = mul (float4(input.Position,1),shadowTransform2);
    output.ShadowCoord3 = mul (float4(input.Position,1),shadowTransform3);
    output.ShadowCoord4 = mul (float4(input.Position,1),shadowTransform4);
    
	//getting the position of the vertex in the world
    float4 posWorld = mul(float4(input.Position,1.0), World);
 
	float3 normal   = mul( input.Normal , WorldInverseTranspose );
	
	float3 lightDir0 = normalize(LightPosition0.xyz - posWorld.xyz);
	
	//use the light direction & normal to determine the intensity
	output.UV = input.UV;
	output.CellUV.xy = float2(1-saturate(dot(lightDir0,normal)),0);	
    return output;
}

float4 mainPS(VS_OUTPUT IN) : COLOR {
	float4 BaseColor = tex2D(CellSampler,IN.CellUV);
	float4 MarkColor = tex2D(MarkSampler,IN.UV);
	
	float4 ShadowColor=float4(1,1,1,1);
	float2 ShadowCoords;		
	
	if (IN.ShadowCoord.z>-1){
		ShadowCoords[0] = IN.ShadowCoord.x/IN.ShadowCoord.w/2.0f+0.5f;
		ShadowCoords[1] = -IN.ShadowCoord.y/IN.ShadowCoord.w/2.0f +0.5f;
		if (length(ShadowCoords-float2(0.5f,0.5f))<=0.71)
			ShadowColor*=tex2D(ShadowSampler,ShadowCoords);
	}

	if (IN.ShadowCoord2.z>-1){
		ShadowCoords[0] = IN.ShadowCoord2.x/IN.ShadowCoord2.w/2.0f+0.5f;
		ShadowCoords[1] = -IN.ShadowCoord2.y/IN.ShadowCoord2.w/2.0f +0.5f;
		if (length(ShadowCoords-float2(0.5f,0.5f))<=0.71)
			ShadowColor*=tex2D(ShadowSampler2,ShadowCoords);
	}

	if (IN.ShadowCoord3.z>-1){
		ShadowCoords[0] = IN.ShadowCoord3.x/IN.ShadowCoord3.w/2.0f+0.5f;
		ShadowCoords[1] = -IN.ShadowCoord3.y/IN.ShadowCoord3.w/2.0f +0.5f;
		if (length(ShadowCoords-float2(0.5f,0.5f))<=0.71)
			ShadowColor*=tex2D(ShadowSampler3,ShadowCoords);
	}
	
	if (IN.ShadowCoord4.z>-1){
		ShadowCoords[0] = IN.ShadowCoord4.x/IN.ShadowCoord4.w/2.0f;
		ShadowCoords[1] = -IN.ShadowCoord4.y/IN.ShadowCoord4.w/2.0f;
		if (length(ShadowCoords)<=0.71)
		{
			ShadowColor*=tex2D(ShadowSampler4,ShadowCoords+float2(0.5f,0.5f));
		}
	}
		
	return BaseColor*MarkColor*IN.Color*ShadowColor;
		
}

technique Specular3Lights {
	pass p0 {
		ZEnable=true;
		ZWriteEnable=true;
		AlphaTestEnable = true;
		AlphaRef = 0.99f;
		AlphaBlendEnable = true;
		//FillMode = Wireframe;
		SRCBLEND = SRCALPHA;
		DESTBLEND = INVSRCALPHA;

		VertexShader = compile vs_3_0 mainVS();
		PixelShader = compile ps_3_0 mainPS();
	}
}